package com.studentmanagement.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.studentmanagement.model.Course;
import com.studentmanagement.model.PageBean;
import com.studentmanagement.service.CourseService;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 显示课程列表
     */
    @GetMapping("")
    public String list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                       @RequestParam(value = "sortField", defaultValue = "courseId") String sortField,
                       @RequestParam(value = "sortOrder", defaultValue = "ASC") String sortOrder,
                       Model model) {
        // 获取分页数据
        PageBean<Course> pageBean = courseService.getCoursesByPage(pageNum, pageSize, sortField, sortOrder);

        // 将数据存入模型
        model.addAttribute("pageBean", pageBean);

        return "course/list";
    }

    /**
     * 显示添加课程表单
     */
    @GetMapping("/add")
    public String showAddForm() {
        return "course/add";
    }

    /**
     * 处理添加课程请求
     */
    @PostMapping("/add")
    public String add(@RequestParam("courseName") String courseName,
                      @RequestParam("creditHours") Integer creditHours,
                      @RequestParam("description") String description,
                      @RequestParam("teacher") String teacher,
                      Model model) {
        // 创建课程对象
        Course course = new Course();
        course.setCourseName(courseName);
        course.setCreditHours(creditHours);
        course.setDescription(description);
        course.setTeacher(teacher);

        // 添加课程
        boolean success = courseService.addCourse(course);

        if (success) {
            // 添加成功，重定向到列表页面
            return "redirect:/course";
        } else {
            // 添加失败，设置错误消息
            model.addAttribute("error", "添加课程失败");
            return "course/add";
        }
    }

    /**
     * 显示编辑课程表单
     */
    @GetMapping("/edit")
    public String showEditForm(@RequestParam("id") Integer courseId, Model model) {
        // 获取课程信息
        Course course = courseService.getCourseById(courseId);

        if (course != null) {
            // 将课程信息存入模型
            model.addAttribute("course", course);

            return "course/edit";
        } else {
            // 课程不存在，重定向到列表页面
            return "redirect:/course";
        }
    }

    /**
     * 处理编辑课程请求
     */
    @PostMapping("/edit")
    public String edit(@RequestParam("courseId") Integer courseId,
                       @RequestParam("courseName") String courseName,
                       @RequestParam("creditHours") Integer creditHours,
                       @RequestParam("description") String description,
                       @RequestParam("teacher") String teacher,
                       Model model) {
        // 创建课程对象
        Course course = new Course();
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        course.setCreditHours(creditHours);
        course.setDescription(description);
        course.setTeacher(teacher);

        // 更新课程
        boolean success = courseService.updateCourse(course);

        if (success) {
            // 更新成功，重定向到列表页面
            return "redirect:/course";
        } else {
            // 更新失败，设置错误消息
            model.addAttribute("error", "更新课程失败");
            model.addAttribute("course", course);
            return "course/edit";
        }
    }

    /**
     * 处理删除课程请求
     */
    @GetMapping("/delete")
    public String delete(@RequestParam("id") Integer courseId) {
        // 删除课程
        courseService.deleteCourse(courseId);

        // 重定向到列表页面
        return "redirect:/course";
    }
}